package com.dnake.utils;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

import com.dnake.panel.R;

import static com.dnake.WeKerApplication.getContext;


/**
 * Created by haoyuliu on 2016/12/five.
 */

public class SoundPlayUtils {
    // SoundPool对象
    public static SoundPool mSoundPlayer = new SoundPool(2,
            AudioManager.STREAM_SYSTEM, 0);
    public static SoundPlayUtils soundPlayUtils;
    // 上下文
    static Context mContext;

    /**
     * 初始化
     *
     * @param context
     */
    public static SoundPlayUtils init(Context context) {
        if (soundPlayUtils == null) {
            soundPlayUtils = new SoundPlayUtils();
        }

        // 初始化声音
        mContext = context;

        mSoundPlayer.load(mContext, R.raw.zero, 1);// one
        mSoundPlayer.load(mContext, R.raw.one, 1);// two
        mSoundPlayer.load(mContext, R.raw.two, 1);// three
        mSoundPlayer.load(mContext, R.raw.three, 1);// four
        mSoundPlayer.load(mContext, R.raw.four, 1);// five
        mSoundPlayer.load(mContext, R.raw.five, 1);// six
        mSoundPlayer.load(mContext, R.raw.six, 1);// seven
        mSoundPlayer.load(mContext, R.raw.seven, 1);// eight
        mSoundPlayer.load(mContext, R.raw.eight, 1);// 9
        mSoundPlayer.load(mContext, R.raw.nine, 1);// 10
        mSoundPlayer.load(mContext, R.raw.no_respone, 1);// 11
        mSoundPlayer.load(mContext, R.raw.user_busy, 1);// 12
        mSoundPlayer.load(mContext, R.raw.open_sucess, 1);// 13
        mSoundPlayer.load(mContext, R.raw.open_fail, 1);// 14
        mSoundPlayer.load(mContext, R.raw.info_erro, 1);// 15
        mSoundPlayer.load(mContext, R.raw.user_no_exist, 1);// 16
        mSoundPlayer.load(mContext, R.raw.brush_id_card, 1);// 17 请刷卡
        mSoundPlayer.load(mContext, R.raw.lose_room_number, 1);// 18 输入房号
        mSoundPlayer.load(mContext, R.raw.user_not_exist, 1);// 19 用户不存在
        mSoundPlayer.load(mContext, R.raw.witness_not_match, 1);// 20 认证不匹配
        return soundPlayUtils;
    }

    /**
     * 播放声音
     *
     * @param soundID
     */
//    public static void play(int soundID) {
//        AudioManager mgr = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
//        float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
//        float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//        float volume = streamVolumeCurrent / streamVolumeMax;
//        mSoundPlayer.play(soundID, volume, volume, 0, 0, 1);
//    }
    /**
     * 播放声音
     *
     * @param soundID
     */
    public static void play(int soundID,int loop) {
        AudioManager mgr = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
        float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
        float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        float volume = streamVolumeCurrent / streamVolumeMax;
        mSoundPlayer.play(soundID, volume, volume, 0, loop, 1);
    }

    /**
     * 停止播放
     * @param soundID
     */
    public static void stop(int soundID){
        mSoundPlayer.stop(soundID);
    }

    /**
     * 免提
     * @param context
     */
    public static void openSpeakerOn(Context context) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        try {
            if (!audioManager.isSpeakerphoneOn())
                audioManager.setSpeakerphoneOn(true);
            audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
            //调节音量
            audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL),0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
